package com.lcu.bos.web.action;

import com.lcu.bos.modle.PageBean;
import com.lcu.bos.modle.Region;
import com.lcu.bos.service.IRegionService;
import com.lcu.bos.untils.PinYin4jUtils;
import com.lcu.bos.web.action.base.BaseAction;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class RegionAction extends BaseAction<Region> {
    @Autowired
    private IRegionService regionService;
    private File excelFile;

    public File getExcelFile() {
        return excelFile;
    }

    public void setExcelFile(File excelFile) {
        this.excelFile = excelFile;
    }

    public  String importExcel() throws IOException {
        System.out.println(excelFile.getAbsoluteFile());//打印临时路径
        //1.解析excel文件中数据  (使用poi jar包)
         //1.1创建Workbook
        HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(excelFile));
        //获取excel表中的sheet
        HSSFSheet sheet = workbook.getSheetAt(0);
        //遍历sheet中的数据  sheet是个迭代器
        List<Region> regions=new ArrayList<>();
        for (Row row:sheet){
            //row.getCell(0).getStringCellValue();
            String id=row.getCell(0).getStringCellValue();
            String province=row.getCell(1).getStringCellValue();
            String city=row.getCell(2).getStringCellValue();
            String district=row.getCell(3).getStringCellValue();
            String postcode=row.getCell(4).getStringCellValue();
            //封装成Region模型
            Region region=new Region(id,province,city,district,postcode);
           // System.out.println("===============================");
            //根据中文生成中文编码        阿帕奇下的StringUtils
            String cityCode= StringUtils.join(PinYin4jUtils.stringToPinyin(city),"");
            //根据中文生产中文简码
            String cityTmp=city.substring(0,city.length()-1);
            String districtTmp=district.substring(0,district.length()-1);
            String [] cityStr=PinYin4jUtils.stringToPinyin(cityTmp);
            String [] districtStr=PinYin4jUtils.stringToPinyin(districtTmp);
            String shortCode=StringUtils.join(cityStr,"")+StringUtils.join(districtStr,"");
            region.setCitycode(cityCode);
            region.setShortcode(shortCode);
           // System.out.println(region);
            regions.add(region);
        }
        //调用service
        regions.remove(0);//排除第一行的数据

        regionService.saveAll(regions);
        responseJson("success");
        return NONE;
    }
    private int page;
    private int rows;

    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public void pageQuery() throws IOException {
        //1.接收参数 page当前页 rows【每页显示多少条数据】
        PageBean<Region> pb=new PageBean<>();
        //2.调用service
        //封装条件
        pb.setCurrentPage(page);
        pb.setPageSize(rows);
        DetachedCriteria cd=DetachedCriteria.forClass(Region.class);//离线查询条件
        pb.setDetachedCriteria(cd);
        regionService.pageQuery(pb);
        //3.返回json格式数据
        JsonConfig config=new JsonConfig();
        //sf-json框架可以把不需要转成json的数据排除掉
        config.setExcludes(new String[]{"currentPage","pageSize","detachedCriteria","subareas"});//排出那些属性不用转成json数据
        JSONObject jb=JSONObject.fromObject(pb,config);
        //创建一个json对象
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Content-Type","text/json;charset=utf-8");
        response.getWriter().write(jb.toString());
    }
    public void listJson() throws IOException {
        //查询所有的区域
        //2.调用service
        List<Region> regions = regionService.findAll();
        //3.返回json格式数据
        JsonConfig config=new JsonConfig();
        //sf-json框架可以把不需要转成json的数据排除掉
        config.setExcludes(new String[]{"subareas"});//排出subareas防止出现死循环
        JSONArray jb=JSONArray.fromObject(regions,config);
        //创建一个json对象
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Content-Type","text/json;charset=utf-8");
        response.getWriter().write(jb.toString());
    }
    @Override
    public String save() {
        return null;
    }

    @Override
    public String delete() throws IOException {
        return null;
    }

    @Override
    public String update() throws UnsupportedEncodingException {
        return null;
    }

    @Override
    public String get() {
        return null;
    }

    @Override
    public String findAll() {
        return null;
    }
}
